package com.startprowry631.model;

import com.startprowry631.bean.WeatherResult;
import com.startprowry631.iface.WeatherIface;
import com.startprowry631.listener.WeatherListener;
import com.startprowry631.retrofitRequest.WeatherRequest;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yu on 2017/6/1.
 */

public class Weather51Model implements WeatherIface{
    Retrofit retrofit;
    private static final String BASE_URL="http://www.weather.com.cn/";

    public Weather51Model(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

    }

    @Override
    public void getWeather(String cityNumber, final WeatherListener listener) {
        WeatherRequest request=retrofit.create(WeatherRequest.class);
        Call<WeatherResult> call=request.getResult(cityNumber);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body().getWeatherinfo());
                }
                else

                {
                    listener.onFail("解析错误");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}